package com.itheima;
import java.util.Comparator;
import java.util.TreeSet;

public class TreeSetDemo {
    //集合只能存储引用数据类型
    public static void main(String[] args) {
        test2();

//        TreeSet<Integer> set = new TreeSet<>();
//        //1.添加数据
//        set.add(1);
//        set.add(2);
//        set.add(3);
//        set.add(4);
//        set.add(5);
//        //2.遍历集合
//        set.forEach(System.out::println);
    }

    public static void test1() {
        TreeSet<Student> set = new TreeSet<>();
        set.add(new Student("zhaoliu", 23));
        set.add(new Student("zhangsan", 20));
        set.add(new Student("lisi", 21));
        set.add(new Student("wangwu", 22));


        for (Student student : set) {
            System.out.println(student.getName());
        }

    }

    public static void test2() {

//    Comparator<Student> comparator=new Comparator<Student>() {
//        @Override
//        public int compare(Student o1, Student o2) {
//            return o1.getAge()-o2.getAge();
//        }
//    };
        //Lambda表达式：
        Comparator<Student> comparator = (o1, o2) -> o2.getAge() - o1.getAge();

        TreeSet<Student> carl = new TreeSet<>(comparator);
        carl.add(new Student("zhaoliu", 23));
        carl.add(new Student("zhangsan", 20));
        carl.add(new Student("lisi", 21));
        carl.add(new Student("wangwu", 22));

        for (Student student : carl) {
            System.out.println(student.getName());
        }

    }


      //可变参数必须是形参的最后一个
    public static int sum(int... a){//可变参数
        int total = 0;
        for(int i = 0; i < a.length; i++){
            total += a[i];
        }
        return total;
    }
}
